import { Modal, Form, Input, Button, Space, message, Radio, Select } from 'antd'
import http from '../api/http'
import type { IEvaluate } from '../types/evaluate'
import type { FormInstance } from 'antd'
import { useEffect, useState } from 'react'


interface GroupType {
  id: string
  name: string
}

interface Props {
  EditModalOpen: boolean
  EditHandleCancel: () => void
  onSuccess: () => void
  editData?: any  // 兼容评估建议表数据结构
}

const Edit: React.FC<Props> = ({ EditModalOpen, EditHandleCancel, onSuccess, editData }) => {
  const [form] = Form.useForm<FormInstance<any>>()  // 使用通用类型
  const [groups, setGroups] = useState<GroupType[]>([
    { id: '肥胖', name: '肥胖' },
    { id: '高血压', name: '高血压' },
    { id: '糖尿病', name: '糖尿病' },
    { id: '正常人群', name: '正常人群' }
  ])

  
  useEffect(() => {
    if (EditModalOpen) {
      // 此处保留原始import的结构，实际无需获取膳食类型
      // 仅作为兼容处理
    }
  }, [EditModalOpen])

 
  useEffect(() => {
    if (editData) {
      // 适配评估建议表字段
      form.setFieldsValue({
        name: editData.name,
        result: editData.result,
        advice: editData.advice,
        state: editData.state,
        remark: editData.remark,
        grp: editData.grp,
        disadvice: editData.disadvice
      })
    }
  }, [editData, form])

  
  const handleSubmit = () => {
    form.validateFields()
      .then(values => {
        if (!editData) {
          message.warning('未获取到待修改的评估数据')
          return
        }

        // 构建评估建议表提交数据
        const submitData = {
          ...values,
          id: editData.id
        }
        
        // 调用评估建议表修改接口
        http.put('evaluate', submitData)
          .then(() => {
            message.success('修改成功')
            EditHandleCancel()
            form.resetFields()
            onSuccess()
          })
          .catch(err => {
            console.error('修改失败:', err)
            message.error('修改失败，请重试')
          })
      })
      .catch(info => {
        console.log('表单验证失败:', info)
        message.warning('请完善必填字段')
      })
  }

  return (
    <Modal
      title="修改评估建议" 
      open={EditModalOpen}
      onCancel={EditHandleCancel}
      centered
      footer={[
        <Button key="cancel" onClick={EditHandleCancel}>
          取消
        </Button>,
        <Button key="submit" type="primary" onClick={handleSubmit}>
          确认修改
        </Button>
      ]}
      destroyOnClose={true}
      width={600}
      disabled={!editData}
    >
      <Form
        form={form}
        layout="vertical"
        initialValues={{}}
      >
        {/* 评估名称 */}
        <Form.Item
          name="name"
          label="评估名称"
          rules={[{ required: true, message: '请输入评估名称' }]}
        >
          <Input placeholder="请输入评估名称" />
        </Form.Item>

        {/* 评估结果 */}
        <Form.Item
          name="result"
          label="评估结果"
          rules={[{ required: true, message: '请输入评估结果' }]}
        >
          <Input placeholder="请输入评估结果" />
        </Form.Item>

        {/* 建议内容 */}
        <Form.Item
          name="advice"
          label="建议内容"
          rules={[{ required: true, message: '请输入建议内容' }]}
        >
          <Input.TextArea rows={4} placeholder="请输入建议内容" />
        </Form.Item>

        {/* 组合分类 */}
        <Form.Item
          name="grp"
          label="组合分类"
          rules={[{ required: true, message: '请选择组合分类' }]}
        >
          <Radio.Group>
            {groups.map(group => (
              <Radio key={group.id} value={group.id}>
                {group.name}
              </Radio>
            ))}
          </Radio.Group>
        </Form.Item>

        {/* 状态设置 */}
        <Space style={{ width: '100%' }}>
          <Form.Item
            name="state"
            label="状态"
            rules={[{ required: true, message: '请选择状态' }]}
            style={{ flex: 1 }}
          >
            <Radio.Group>
              <Radio value={0}>启用</Radio>
              <Radio value={1}>禁用</Radio>
            </Radio.Group>
          </Form.Item>

          <Form.Item
            name="disadvice"
            label="建议状态"
            rules={[{ required: true, message: '请选择建议状态' }]}
            style={{ flex: 1 }}
          >
            <Radio.Group>
              <Radio value={0}>启用</Radio>
              <Radio value={1}>禁用</Radio>
            </Radio.Group>
          </Form.Item>
        </Space>

        {/* 备注 */}
        <Form.Item
          name="remark"
          label="备注"
        >
          <Input.TextArea rows={3} placeholder="请输入备注信息（可选）" />
        </Form.Item>
      </Form>
    </Modal>
  )
}

export { Edit }